package cn.donting.jme.expansion.inspect;

import cn.donting.jme.EditorContent;
import cn.donting.jme.event.NameChangeEvent;
import cn.donting.jme.expansion.attribute.BaseInfoAttributeGroup;
import cn.donting.jme.expansion.attribute.TitlePaneAttributeGroup;
import cn.donting.jme.expansion.attribute.lmpl.EnumAttribute;
import cn.donting.jme.expansion.attribute.lmpl.IntegerAttribute;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.ioc.Sort;
import cn.donting.jme.utile.IconUtil;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;

@Bean
@Sort(0)
public class SpatialBasInfoInspectBuilder implements InspectBuilder<Spatial> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof Spatial;
    }

    @Override
    public void builder(Inspect inspect, Spatial spatial) {
        BaseInfoAttributeGroup baseInfoAttributeGroup = new BaseInfoAttributeGroup();
        baseInfoAttributeGroup.setType(spatial.getClass().getName());
        baseInfoAttributeGroup.setName(spatial.getName());
        baseInfoAttributeGroup.setIcon(IconUtil.getIcon(spatial, BaseInfoAttributeGroup.ICON_SIZE));
        baseInfoAttributeGroup.setNameChange(newValue -> {
            spatial.setName(newValue);
            EditorContent.postEvent(new NameChangeEvent(baseInfoAttributeGroup, spatial));
        });
        inspect.setTop(baseInfoAttributeGroup);

        TitlePaneAttributeGroup baseAttributeGroup = new TitlePaneAttributeGroup("基础属性");

        EnumAttribute<Spatial.CullHint> cullHintEnumAttribute = new EnumAttribute<>("CullHint:", Spatial.CullHint.class);
        cullHintEnumAttribute.setValue(spatial.getCullHint());
        cullHintEnumAttribute.setValueChangeListener(newValue -> spatial.setCullHint(newValue));
        baseAttributeGroup.addAttribute(cullHintEnumAttribute);

        EnumAttribute<Spatial.BatchHint> batchHintEnumAttribute = new EnumAttribute<>("BatchHint:", Spatial.BatchHint.class);
        batchHintEnumAttribute.setValue(spatial.getBatchHint());
        batchHintEnumAttribute.setValueChangeListener(newValue -> spatial.setBatchHint(newValue));
        baseAttributeGroup.addAttribute(batchHintEnumAttribute);

        EnumAttribute<RenderQueue.ShadowMode> shadowModeEnumAttribute = new EnumAttribute<>("ShadowMode:", RenderQueue.ShadowMode.class);
        shadowModeEnumAttribute.setValue(spatial.getShadowMode());
        shadowModeEnumAttribute.setValueChangeListener(newValue -> spatial.setShadowMode(newValue));
        baseAttributeGroup.addAttribute(shadowModeEnumAttribute);

        EnumAttribute<RenderQueue.Bucket> bucketEnumAttribute = new EnumAttribute<>("Bucket:", RenderQueue.Bucket.class);
        bucketEnumAttribute.setValue(spatial.getQueueBucket());
        bucketEnumAttribute.setValueChangeListener(newValue -> spatial.setQueueBucket(newValue));
        baseAttributeGroup.addAttribute(bucketEnumAttribute);

        if (spatial instanceof Geometry) {
            Geometry geo = ((Geometry) spatial);
            IntegerAttribute loaLevelAttribute = new IntegerAttribute("LodLevel:");
            if(geo.getMesh().getNumLodLevels()==0){
                loaLevelAttribute.setEnable(false);
            }
            loaLevelAttribute.setValue(((Geometry) spatial).getLodLevel());
            loaLevelAttribute.setValueChangeListener(newValue -> spatial.setLodLevel(newValue));
            loaLevelAttribute.setMixValue(0);
            loaLevelAttribute.setMaxValue(geo.getMesh().getNumLodLevels() == 0 ? 0 : geo.getMesh().getNumLodLevels() - 1);
            baseAttributeGroup.addAttribute(loaLevelAttribute);
        }

        inspect.addAttributeGroup(baseAttributeGroup);
    }
}
